﻿using Demo.Interface;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using PracticalDemo.Utility.Filter;
using System;

namespace PracticalDemo.Controllers
{
    /// <summary>
    /// AOP面向切面编程   Aspect Oriented Programming
    /// 1、聚焦业务逻辑，轻松扩展功能
    /// 2、代码复用，集中管理
    /// 
    /// Filter三种注册：Action Controller 全局
    /// Filter四种注入：全局注册 ServiceFilter TypeFilter IFilterFactory
    /// 
    /// 面向环形编程
    /// Filter MVC 流程，流程外处理不了(404)
    /// 中间件 任何请求最外层到达，提前做些筛选过滤工作。但是中间件一般没有控制器action这些信息，不适合业务逻辑
    /// Autofac 可以深入到业务逻辑层做扩展
    /// 
    /// </summary>

    [ServiceFilter(typeof(CustomExceptionFilterAttribute))]  //ServiceFilter
    //[TypeFilter(typeof(CustomExceptionFilterAttribute))]
    [CustomFilterFactory(typeof(CustomExceptionFilterAttribute))]
    [CustomControllerFilter]
    //[CustomExceptionFilterAttribute]  //控制器注册
    public class FourthController : Controller
    {
        #region Identity
        private readonly ILogger<FourthController> _logger;
        private readonly ITestServiceA _iTestServiceA;
        private readonly ITestServiceB _iTestServiceB;
        private readonly ITestServiceC _iTestServiceC;
        private readonly ITestServiceD _iTestServiceD;
        private readonly ITestServiceE _iTestServiceE;
        private readonly IA _ia;
        public FourthController(ILogger<FourthController> logger
            , ITestServiceA testServiceA
             , ITestServiceB testServiceB
            , ITestServiceC testServiceC
            , ITestServiceD testServiceD
            , ITestServiceE testServiceE
            , IA a)
        {
            this._logger = logger;
            this._iTestServiceA = testServiceA;
            this._iTestServiceB = testServiceB;
            this._iTestServiceC = testServiceC;
            this._iTestServiceD = testServiceD;
            this._iTestServiceE = testServiceE;
            this._ia = a;
        }
        #endregion

        //[CustomExceptionFilterAttribute] //action注册 不能直接用，需要自己写
        public IActionResult Index()
        {
            this._logger.LogInformation("12345648658686");
            //try catch
            int i = 1;
            int k = 3;
            int m = i + k;//业务逻辑
            int l = m - m;
            int j = m / l;//异常

            return View();
        }

        public IActionResult Info()
        {
            //try catch
            int i = 1;
            int k = 3;
            int m = i + k;//业务逻辑
            int l = m - m;
            int j = m / l;//异常

            return View();
        }

        [CustomActionFilterAttribute]
        //[CustomActionFilterAttribute(Order=123)]
        public IActionResult Infomation()
        {
            Console.WriteLine("This is Infomation Action");
            this._ia.Show(123, "简简单单");
            return View();
        }
    }
}
